package com.aiti.lulian.service.impl;

import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.gov.CardGovProjectDto;
import com.aiti.lulian.entity.gov.CardGovProject;
import com.aiti.lulian.entity.gov.CardGovProjectApplication;
import com.aiti.lulian.mapper.gov.CardGovProjectApplicationMapper;
import com.aiti.lulian.mapper.gov.CardGovProjectMapper;
import com.aiti.lulian.service.ICardGovProjectService;
import com.aiti.lulian.vo.gov.CardGovProjectVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CardGovProjectServiceImpl extends BaseSerivceImpl<CardGovProjectMapper, CardGovProject> implements ICardGovProjectService {

    @Autowired
    private CardGovProjectApplicationMapper cardGovProjectApplicationMapper;

    @Override
    public List<CardGovProjectVo> getProjectList(CardGovProjectDto dto, Page<CardGovProject> page) {
        return this.baseMapper.selectProjectList(dto, page);
    }

    @Override
    public CardGovProjectVo getProjectDetails(String baseId) {
        return this.baseMapper.selectProjectDetails(baseId);
    }

    @Override
    public void addProject(CardGovProjectDto dto) {
        CardGovProject project = new CardGovProject();
        BeanUtils.copyProperties(dto, project);
        project.setCreator(dto.getUserId());
        project.setBaseCreateTime(new Date());
        this.save(project);
    }

    @Override
    public void updateProject(CardGovProjectDto dto) {
        CardGovProject project = new CardGovProject();
        BeanUtils.copyProperties(dto, project);
        project.setUpdateBy(dto.getUserId());
        project.setBaseUpdateTime(new Date());
        this.updateById(project);
    }

    @Override
    public Message deleteProject(CardGovProjectDto dto) {
        String projectId = dto.getBaseId();
        List<CardGovProjectApplication> cardGovProjectApplications = cardGovProjectApplicationMapper.selectList(new LambdaQueryWrapper<CardGovProjectApplication>().eq(CardGovProjectApplication::getProjectId, projectId));
        if (cardGovProjectApplications.size() > 0) {
            return Message.fail("当前项目存在申报记录，无法删除");
        } else {
            this.removeById(projectId);
            return Message.success();
        }
    }
}
